<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Image;
class KeywordsController extends PublicController {


	function _initialize() {
		parent::_initialize ();
	}
	//代理商列表
    public function index(){
        $header = C('header')['keywords'];
        if(IS_POST){
            $data = I('post.');
            $keyword = $data['keyword'];
            $keywordList = explode("\n", $keyword);
            $querylist['logtime'] = time();
            $querylist['kwd'] = $keyword;
            $querylist['admin_id'] = 5;
            $query = M('query')->add($querylist);
            foreach ($keywordList as $k => $v) {
                $datalist[] = array(
                    'admin_id' => 5,
                    'Keyword' => $v,
                    'baiduipc' => rand(1,35),
                    'baiduim' => rand(1,35),
                    '360pc' => rand(1,35),
                    '360m' => rand(1,35),
                    'sogoupc' => rand(1,35),
                    'sogoum' => rand(1,35),
                    'logtime' => time(),
                    'query_id' => intval($query),
                    'first_rank' => rand(25,135),
                );
            }

           $kwd = M('keyword')->addAll($datalist);
           $where['query_id'] = $query;
           $list = M('keyword')->where($where)->field('Keyword,baiduipc,baiduim,360pc,360m,sogoupc,sogoum,baiduipc,baiduim')->select();
        }else{
            
            $count = 0;
            $list = [];

        }

        $this->assign("keyword", $keyword);
        $this->assign("header", $header);
        $this->assign("list", $list);
        layout('Layout/twolayout');
        $this->display();
    }


    public function queryLog(){
        $search = I('search');
        if($search){
            $where['user_name'] = array('like',"%$search%");
            $this->assign("search", $search);
        }

        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        $header = C('header')['queryLog'];
        $where['status'] = 0;
        $count = M('query')->where($where)->count();
       


        
        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出

        $list = M('query')->where($where)->field('id,logtime,kwd')->order('admin_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($list as $k => $v) {
            $list[$k]['logtime'] = date('Y-m-d H:i:s',$v['logtime']);
        }

        // var_dump($show);exit;
        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        // echo  htmlentities($show,ENT_QUOTES,"UTF-8");exit;
        // $show = preg_replace('//s*/', '', $show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        layout('Layout/twolayout');
        $this->display();
    }


    public function details(){
        $query_id = I('query_id');
        if(!$query_id){
           $this->error('信息错误');exit;
        }

        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        $header = C('header')['orders'];
        $where['status'] = 0;
        $where['query_id'] = $query_id;
        $count = M('keyword')->where($where)->count();
       

        $users = M('user')->field('user_id,user_name')->select();
        
        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出

        $list = M('keyword')->where($where)->field('id,Keyword,baiduipc,baiduim,360pc,360m,sogoupc,sogoum')->order('admin_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        // var_dump($show);exit;
        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        // echo  htmlentities($show,ENT_QUOTES,"UTF-8");exit;
        // $show = preg_replace('//s*/', '', $show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("query_id", $query_id);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        $this->assign("users", $users);
        layout('Layout/twolayout');
        $this->display();
    }


    public function del(){
        $del = I('del');
        if(!$del){
           $this->error('信息错误');
        }
        $where['id'] = $del;
        $list = M('keyword')->where($where)->delete();
         if($list){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }



    public function queryList(){
       if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        $header = C('header')['orders'];
        $where['status'] = 0;
        $count = M('keyword')->where($where)->count();
       


        
        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出

        $list = M('keyword')->where($where)->field('id,Keyword,baiduipc,baiduim,360pc,360m,sogoupc,sogoum')->order('admin_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        // var_dump($show);exit;
        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        // echo  htmlentities($show,ENT_QUOTES,"UTF-8");exit;
        // $show = preg_replace('//s*/', '', $show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        layout('Layout/twolayout');
        $this->display();
    }




    public function addSite(){
        if(IS_POST){
            $data = I('post.');
            if(empty($data['query_id']) || empty($data['seo']) || empty($data['site']) || empty($data['ftp_ip']) || empty($data['username']) || empty($data['password']) || empty($data['adminadd']) || empty($data['adminusername']) || empty($data['adminpassword']) || empty($data['user_id'])){
                $this->error('请填写完整信息');
            }else{
                $res['site'] = $data['site'];
                $res['ftp_ip'] = $data['ftp_ip'];
                $res['username'] = $data['username'];
                $res['adminadd'] = $data['adminadd'];
                $res['adminusername'] = $data['adminusername'];
                $res['adminpassword'] = $data['adminpassword'];
                $res['password'] = $data['password'];
                $res['query_id'] = $data['query_id'];
                $res['admin_id'] = 5; //$res['admin_id'] = $_SESSION ["userInfo"]['admin_id'];
                $res['seo'] = json_encode($data['seo']);

                $list = M('site')->add($res);
                if($list){
                    $this->success("添加成功",U("Keywords/queryLog"));
                }else{
                    $this->error("添加失败");
                }
            }
        }else{
            $this->error('信息错误');
        }
    }
}